/*
 * Copyright (C) 2014 Benny Bobaganoosh
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.base.engine.rendering;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL13.GL_TEXTURE0;
import static org.lwjgl.opengl.GL13.glActiveTexture;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.HashMap;

import javax.imageio.ImageIO;

import com.base.engine.core.Util;
import com.base.engine.rendering.resourceManagement.TextureResource;


public class Texture
{
	private static HashMap<String, TextureResource> loadedTextures = new HashMap<String, TextureResource>();
	private TextureResource resource;
	private String          fileName;
	
	
	private static int DEFAULT_TEXTURE_TARGET=GL_TEXTURE_2D;
	private static int DEFAULT_FILTER=GL_LINEAR;
	private static int DEFAULT_ATTACHMENT=GL_NONE;
	
	public Texture(String fileName){
		this(fileName, DEFAULT_TEXTURE_TARGET, DEFAULT_FILTER, new int[]{DEFAULT_ATTACHMENT});
	}
	
	public Texture(String fileName, int textureTarget, int filter, int[] attachments)
	{
		this.fileName = fileName;
		TextureResource oldResource = loadedTextures.get(fileName);

		if(oldResource != null)
		{
			resource = oldResource;
			resource.addReference();
		}
		else
		{
			BufferedImage image;
			try {
				image = ImageIO.read(new File("./res/textures/" + fileName));
				ByteBuffer imageData = toByteBuffer(image);
				int width=image.getWidth();
				int height=image.getHeight();
				
				resource=new TextureResource(textureTarget, width,height, 1, new ByteBuffer[]{imageData}, new int[]{filter}, attachments);
				
//				resource = loadTexture(fileName);
				loadedTextures.put(fileName, resource);
			} catch (IOException e) {
				System.err.println("could not read image file ./res/textures/" + fileName);
				e.printStackTrace();
				System.exit(1);
			}
		}
	}
	
	/**
	 * 
	 * @param width
	 * @param height
	 * @param imageData a width*height*4 byteBuffer which is flipped
	 * @param textureTarget
	 * @param filter
	 */
	public Texture(int width, int height, ByteBuffer imageData, int textureTarget, int[] filters, int[] attachments){
		fileName="";
		resource=new TextureResource(textureTarget, width,height, 1, new ByteBuffer[]{imageData}, filters, attachments);
	}
	
	public Texture(int width, int height){
		fileName="";
		
		ByteBuffer imageData= Util.createByteBuffer(width*height * 4);
		for(int i=0; i<width; i++){
			for(int j=0;j<height; j++){
				imageData.put((byte)0x0);
				imageData.put((byte)0x0);
				imageData.put((byte)0x0);
				imageData.put((byte)0x0);
			}
		}
		imageData.flip();
		resource=new TextureResource(DEFAULT_TEXTURE_TARGET, width,height, 1, new ByteBuffer[]{imageData}, new int[]{DEFAULT_FILTER}, new int[]{DEFAULT_ATTACHMENT});
	}
	
	public Texture(int width, int height, int textureTarget, int[] filters, int[] attachments){
		fileName="";
		
		ByteBuffer imageData= Util.createByteBuffer(width*height * 4);
		for(int i=0; i<width; i++){
			for(int j=0;j<height; j++){
				imageData.put((byte)0x0);
				imageData.put((byte)0x0);
				imageData.put((byte)0x0);
				imageData.put((byte)0x0);
			}
		}
		imageData.flip();
		resource=new TextureResource(textureTarget, width,height, 1, new ByteBuffer[]{imageData}, filters, attachments);
	}

	@Override
	protected void finalize()
	{
		if(resource.removeReference() && !fileName.isEmpty())
		{
			loadedTextures.remove(fileName);
		}
	}

	public void bind(int samplerSlot)
	{
		assert(samplerSlot >= 0 && samplerSlot <= 31);
		glActiveTexture(GL_TEXTURE0 + samplerSlot);
		resource.bind(0);
	}
	
	public void bindAsRenderTarget(){
		//TODO:
	}
	
//	private static TextureResource loadTexture(String fileName)
//	{
//		try
//		{
//			BufferedImage image = ImageIO.read(new File("./res/textures/" + fileName));
//			ByteBuffer buffer = toByteBuffer(image);
//
//			TextureResource resource = new TextureResource();
//			glBindTexture(GL_TEXTURE_2D, resource.getId());
//
//			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
//			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//
//			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
//			glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//
//			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
//
//			return resource;
//		}
//		catch(Exception e)
//		{
//			e.printStackTrace();
//			System.exit(1);
//		}
//		
//		return null;
//	}

	private static ByteBuffer toByteBuffer(BufferedImage image) {
		int[] pixels = image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth());

		ByteBuffer buffer = Util.createByteBuffer(image.getHeight() * image.getWidth() * 4);
		boolean hasAlpha = image.getColorModel().hasAlpha();

		for(int y = 0; y < image.getHeight(); y++)
		{
			for(int x = 0; x < image.getWidth(); x++)
			{
				int pixel = pixels[y * image.getWidth() + x];

				buffer.put((byte)((pixel >> 16) & 0xFF));
				buffer.put((byte)((pixel >> 8) & 0xFF));
				buffer.put((byte)((pixel) & 0xFF));
				if(hasAlpha)
					buffer.put((byte)((pixel >> 24) & 0xFF));
				else
					buffer.put((byte)(0xFF));
			}
		}

		buffer.flip();
		return buffer;
	}
}
